<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 */
 
if($_SESSION['rank'] < 2){
	exit(Lang::getBackGlobal('wrong_permissions'));
}
 
function manager_fileinfo($file){
$perms = fileperms($file);
	if (($perms & 0xC000) == 0xC000) {
    // Socket
    $info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
    // Symbolic Link
    $info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
    // Regular
    $info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
    // Block special
    $info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
    // Directory
    $info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
    // Character special
    $info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
    // FIFO pipe
    $info = 'p';
} else {
    // Unknown
    $info = 'u';
}

// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
            (($perms & 0x0800) ? 's' : 'x' ) :
            (($perms & 0x0800) ? 'S' : '-'));

// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
            (($perms & 0x0400) ? 's' : 'x' ) :
            (($perms & 0x0400) ? 'S' : '-'));

// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
            (($perms & 0x0200) ? 't' : 'x' ) :
            (($perms & 0x0200) ? 'T' : '-'));
return $info;
}
 
if(empty($_GET['dir'])){
	$root = "../public/";	
}else{
	$_GET['dir'] = str_replace("../", " ", $_GET['dir']);
	$_GET['dir'] = str_replace("..", " ", $_GET['dir']);
	$root = "../public/".$_GET['dir'].'/';
}

if(!empty($_FILES['newfile']['name'])){
    $disallowed_ext = array('php', 'php4', 'php5', 'php3', 'php2');
    if(!in_array(strtolower(pathinfo($_FILES['newfile']['name'], PATHINFO_EXTENSION)), $disallowed_ext)){

	$folder = $root;
    $result = $folder . "/".$_FILES['newfile']['name'];
    $file = $_FILES['newfile']['tmp_name'];
    $copy = @move_uploaded_file($file, $result);
    @chmod ($result, 0777);
    if($copy === false){
    	echo '<p>'.Lang::getBackGlobal('filemanager_fail').'</p><p>'.Lang::getBackGlobal('filemanager_fail2').'</p>';
    }else{
    	echo '<p>'.Lang::getBackGlobal('filemanager_done').'</p>';
    }
            
    }else{
        echo '<p>'.Lang::getBackGlobal('filemanager_extension').'<strong>.'.pathinfo($_FILES['newfile']['name'], PATHINFO_EXTENSION).'</strong> !</p>';
    }
}

if(!empty($_POST['newfolder'])){
	$folder = str_replace(array("..", "."), array(" ", " "), $_POST['newfolder']);
	if(mkdir($root."/".$folder, 0777)){
		echo "<p>".Lang::getBackGlobal('filemanager_folder')."</p>";
	}else{
		echo "<p>".Lang::getBackGlobal('filemanager_folder2')."</p>\n";
	}
}

$tmp = (empty($_GET['dir'])) ? '' : $_GET['dir'].'/';

if($_GET['a'] == 'delete'){
	unlink($_GET['file']);
	echo '<meta http-equiv="refresh" content="1;url=index.php?p=filemanager&dir='.$root.'">';
}

$dirs = dirlist($root, "dirs");
sort($dirs);
$files = dirlist($root, "files");
sort($files);

$link2 = str_replace("administrator/", "", $GLOBALS['url']);

echo '<table>';
echo "\n<tr><th>".Lang::getBackGlobal('filemanager_name')."</th><th>".Lang::getBackGlobal('filemanager_size')."</th><th>".Lang::getBackGlobal('filemanager_change')."</th><th>".Lang::getBackGlobal('filemanager_chmod')."</th><th>".Lang::getBackGlobal('action')."</th></tr>";
foreach($dirs as $key => $value){
	echo '<tr><td><a href="index.php?p=filemanager&dir='.$tmp.$value.'"><img src="../libs/icons/folder.png" alt="folder" /> '.$value.'</a></td><td>&lt;dir&gt;</td><td>&lt;dir&gt;</td><td>'.manager_fileinfo($root.$value).'</td><td></td></tr>';
}
foreach($files as $key => $value){
	echo '<tr><td><a href="'.$link2.'public/'.$tmp.$value.'"><img src="../libs/icons/picture_empty.png" alt="file" /> '.$value.'</a></td><td>'.filesize($root.$value).' bytes</td><td>'.date ("F d Y H:i:s", filemtime($root.$value)).'</td><td>'.manager_fileinfo($root.$value).'</td><td><a href="index.php?p=filemanager&a=delete&file='.$root.$value.'">'.Lang::getBackGlobal('filemanager_delete').'</a></td></tr>';
}
echo '</table>';

echo "<br />\n<table>";
echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">\n";
echo "<tr><td><h2><img src=\"../libs/icons/add.png\" alt=\"add\" /> ".Lang::getBackGlobal('filemanager_file_upload')."</h2></td></tr>\n";
echo "<tr><td><input type=\"file\" name=\"newfile\" size=\"50\" /></td><td><input type=\"submit\" value=\"".Lang::getBackGlobal('filemanager_upload')."\" /></td></tr>\n";
echo "</form>\n</table>";

echo "<br />\n<table>";
echo "<form action=\"\" method=\"post\">\n";
echo "<tr><td><h2><img src=\"../libs/icons/add.png\" alt=\"add\" /> ".Lang::getBackGlobal('filemanager_folder_upload')."</h2></td></tr>\n";
echo "<tr><td><input type=\"text\" name=\"newfolder\" size=\"50\" /></td><td><input type=\"submit\" value=\"".Lang::getBackGlobal('filemanager_create')."\" /></td></tr>\n";
echo "</form>\n</table>";

echo '<p><a href="#" onclick="history.back()">&lt;- '.Lang::getBackGlobal('back').'</a></p>';
?>